﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace HelperPrototype.TabPageControls
{
    public partial class QuanlyReceiptGroup : UserControl
    {
        public QuanlyReceiptGroup()
        {
            InitializeComponent();
        }

         private void loadData()
        {
            dgvGroup.Rows.Clear();

            _DB.DataClasses1DataContext dc = new _DB.DataClasses1DataContext();
            var itemGroups = from ig in dc.ReceiptGroups select ig;
            int count = 1;
            foreach (_DB.ReceiptGroup ig in itemGroups)
            {
                if (ig.deleted == true)
                    continue;
                DataGridViewRow row = new DataGridViewRow();
                DataGridViewCell c = new DataGridViewTextBoxCell();
                c.Value = count.ToString();
                row.Cells.Add(c);
                c = new DataGridViewTextBoxCell();
                c.Value = ig.ReceiptGroup_Name;
                row.Cells.Add(c);
                row.Tag = ig.ReceiptGroup_ID;
                dgvGroup.Rows.Add(row);
                count++;
            }
        }

        private void btnRefresh_Click(object sender, EventArgs e)
        {
            loadData();
        }

       

        private void btnAdd_Click(object sender, EventArgs e)
        {
            OtherForms.ThemReceiptGroupForm f = new OtherForms.ThemReceiptGroupForm();
            f.ShowDialog();
            loadData();
        }

        private void btnClose_Click(object sender, EventArgs e)
        {
            Main m = (Main)this.Tag;
            m.closeTab();
        }

        private void btnView_Click(object sender, EventArgs e)
        {
            if (dgvGroup.SelectedRows.Count==0)
            {
                return;
            }
            int id =(int) dgvGroup.SelectedRows[0].Tag;

            OtherForms.ThemReceiptGroupForm f = new OtherForms.ThemReceiptGroupForm(id,0);
            f.ShowDialog();
        }

        private void btnEdit_Click(object sender, EventArgs e)
        {
            if (dgvGroup.SelectedRows.Count == 0)
            {
                return;
            }
            int id = (int)dgvGroup.SelectedRows[0].Tag;

            OtherForms.ThemReceiptGroupForm f = new OtherForms.ThemReceiptGroupForm(id, 1);
            f.ShowDialog();
            loadData();
        }

        private void QuanlyReceiptGroup_Load(object sender, EventArgs e)
        {
            loadData();
        }

        private void btnDel_Click(object sender, EventArgs e)
        {
            if (dgvGroup.SelectedRows.Count == 0)
            {
                return;
            }
            int id = (int)dgvGroup.SelectedRows[0].Tag;
            _DB.ReceiptGroupDB.delete(id);
            loadData();
        }
    }

    
}
